<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'orther:device:deviceInfo:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ deviceSerial: record.deviceSerial })">
          {{ record.deviceSerial }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsOrtherDeviceDeviceInfoList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { deviceInfoDelete, deviceInfoListData } from '/@/api/orther/device/deviceInfo';
  import { deviceInfoDisable, deviceInfoEnable } from '/@/api/orther/device/deviceInfo';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { usePermission } from '/@/hooks/web/usePermission';
  const { hasPermission } = usePermission();

  const { t } = useI18n('orther.device.deviceInfo');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('设备档案管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('设备序列号'),
        field: 'deviceSerial',
        component: 'Input',
      },
      {
        label: t('设备名称'),
        field: 'deviceName',
        component: 'Input',
      },
      {
        label: t('设备位置'),
        field: 'deviceLocation',
        component: 'Input',
        ifShow: false,
      },
      {
        label: t('UIKIT主题'),
        field: 'uikitTemplate',
        component: 'Select',
        componentProps: {
          dictType: 'zf_device_uikit',
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'sys_search_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('设备序列号'),
      dataIndex: 'deviceSerial',
      key: 'a.device_serial',
      sorter: true,
      width: 150,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('设备名称'),
      dataIndex: 'deviceName',
      key: 'a.device_name',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('设备位置'),
      dataIndex: 'deviceLocation',
      key: 'a.device_location',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('appkey'),
      dataIndex: 'appkey',
      key: 'a.appkey',
      sorter: true,
      width: 200,
      align: 'left',
      ifShow: hasPermission(['orther:device:deviceInfo:edit']),
    },
    {
      title: t('secret'),
      dataIndex: 'secret',
      key: 'a.secret',
      sorter: true,
      width: 200,
      align: 'left',
      ifShow: hasPermission(['orther:device:deviceInfo:edit']),
    },
    {
      title: t('播放地址(高清)'),
      dataIndex: 'previewUrlHd',
      key: 'a.preview_url_hd',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('播放地址(流畅)'),
      dataIndex: 'previewUrlSd',
      key: 'a.preview_url_sd',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('UIKIT主题模板'),
      dataIndex: 'uikitTemplate',
      key: 'a.uikit_template',
      dictType: 'zf_device_uikit',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'sys_search_status',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑设备档案'),
        onClick: handleForm.bind(this, { deviceSerial: record.deviceSerial }),
        auth: 'orther:device:deviceInfo:edit',
      },
      {
        icon: 'ant-design:stop-outlined',
        color: 'error',
        title: t('停用设备档案'),
        popConfirm: {
          title: t('是否确认停用设备档案'),
          confirm: handleDisable.bind(this, { deviceSerial: record.deviceSerial }),
        },
        auth: 'orther:device:deviceInfo:edit',
        ifShow: () => record.status === '0',
      },
      {
        icon: 'ant-design:check-circle-outlined',
        color: 'success',
        title: t('启用设备档案'),
        popConfirm: {
          title: t('是否确认启用设备档案'),
          confirm: handleEnable.bind(this, { deviceSerial: record.deviceSerial }),
        },
        auth: 'orther:device:deviceInfo:edit',
        ifShow: () => record.status === '2',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除设备档案'),
        popConfirm: {
          title: t('是否确认删除设备档案'),
          confirm: handleDelete.bind(this, { deviceSerial: record.deviceSerial }),
        },
        auth: 'orther:device:deviceInfo:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: deviceInfoListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDisable(record: Recordable) {
    const res = await deviceInfoDisable(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleEnable(record: Recordable) {
    const res = await deviceInfoEnable(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleDelete(record: Recordable) {
    const res = await deviceInfoDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
